package com.fgba.express.util;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;

/**
 * 快递取件码生成
 */
public class ExpressCodeGenerate {
    /**
     * 默认为纯数字，如果需加入字母就改成0123456789abcdefg...........
     */
    private static final String SYMBOLS = "0123456789";

    private static final Random RANDOM = new SecureRandom();

    public static String getPickupCode(List<String> list) {
        String sixStr = getSixStr();
        boolean contains = list.contains(sixStr);
        if (contains || sixStr.length() != 6) {
            sixStr = getPickupCode(list);
        }
        return sixStr;
    }

    /**
     * 获取长度为 6 的随机数字
     *
     * @return 随机数字
     */
    public static String getSixStr() {
        char[] nonceChars = new char[6];  //指定长度，自己可以设置

        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }
}
